<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Pguestbook block.
 *
 * @package    block_pguestbook
 * @copyright  2014 Polikarpov Alexander
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
 
require_once(realpath(dirname(__FILE__)).'/lib.php');

class block_pguestbook extends block_base {
    
    function init() {
        $this->title = get_string('pluginname', 'block_pguestbook');
    }
    
    public function get_content() {
        
        global $CFG, $OUTPUT, $USER;
        
        $lib = new block_pguestbook_manager();
        // Подключаем javascript
        $this->page->requires->js( new moodle_url(
            $CFG->wwwroot . '/blocks/pguestbook/js/jquery-2.1.1.min.js'));
        $this->page->requires->js( new moodle_url(
            $CFG->wwwroot . '/blocks/pguestbook/js/jquery.autosize.min.js'));
        $this->page->requires->js( new moodle_url(
            $CFG->wwwroot . '/blocks/pguestbook/js/pguestbook.js'));
        
        if ($this->content !== null) {
            return $this->content;
        }
        // Получаем имя пользователя
        if($USER->id) {
            $username = $USER->username;
        } else {
            $username = get_string('guest', 'block_pguestbook');
        }
        // Массив всех администраторов для проверки прав.
        $isadmin = is_siteadmin();
        
        $task = optional_param('pguestbook_task', '', PARAM_TEXT);
        // Выполняем задачу task
        switch ($task) 
        {
            case 'addcomment' :
                $lib->add();
                break;
            case 'addpost' : 
              $lib->add();
                break;
            case 'changepost' : 
               $lib->changepost();
                break;
            case 'changecomment' : 
               $lib->changecomment();
                break;
            case 'deletepost' : 
               $lib->deletepost();
               break;
            case 'deletecomment' : 
               $lib->deletecomment();
               break;
            default: 
               break;
        }
        // Формируем контент блока
        $this->content         =  new stdClass;
        $this->content->text  = html_writer::tag(
            'div', 
            get_string('addpost', 'block_pguestbook'), 
            array('id' => 'pguestbook_addform_button')
        );
        $this->content->text .= html_writer::start_tag(
            'div', 
            array('id' => 'pguestbook_addform_wrapperbg')
        );
        $this->content->text .= html_writer::start_tag(
            'div', 
            array('id' => 'pguestbook_addform_wrapper')
        );
        $this->content->text .= html_writer::start_tag(
            'div', 
            array('id' => 'pguestbook_addform')
        );
        $this->content->text .= html_writer::start_tag(
            'div', 
            array('id' => 'pguestbook_form')
        );
        $this->content->text .= 
            '<input type="text" name="autor" id="pguestbook_autorform" 
                value="'.$username.'" 
                placeholder="'.get_string("name", "block_pguestbook").'" 
            />';
        $this->content->text .= html_writer::tag(
            'span', 
            get_string('error_fields', 'block_pguestbook'), 
            array('id' => 'pguestbook_form_msg')
        );
        $this->content->text .= '<br />';
        $this->content->text .= html_writer::tag(
            'div', 
            get_string('label_post_text', 'block_pguestbook'), 
            array()
        );
        $this->content->text .= 
            '<textarea rows="10" class="pguestbook_text" id="pguestbook_textform" name="text">';
        $this->content->text .= '</textarea>';
        $this->content->text .= 
            '<input type="hidden" name="id" id="pguestbook_idform" value="0" />';
        $this->content->text .= 
            '<input type="hidden" name="task" id="pguestbook_taskform" value="" />';
        $this->content->text .= html_writer::tag(
            'div', 
            '', 
            array('style' => 'clear: both;')
        );
        $this->content->text .= html_writer::tag(
            'div', 
            get_string('send', 'block_pguestbook'), 
            array('id' => 'pguestbook_submit')
        );
        $this->content->text .= html_writer::end_tag('div');
        $this->content->text .= html_writer::end_tag('div');
        $this->content->text .= html_writer::end_tag('div');
        $this->content->text .= html_writer::end_tag('div');
        $this->content->text .= html_writer::start_tag(
            'div', 
            array('id' => 'pguestbook_postlist', 'class' => 'pguestbook_postlist')
        );
        $result = $lib->getall();
        $id = null;
        // Вывод сообщений и комментариев к ним
        foreach ($result as $row) 
        {
            // Сообщение
            if ($id != $row['0']) 
            {
                $id = $row['0'];
                $this->content->text .= html_writer::start_tag(
                    'div', 
                    array('class' => 'pguestbook_postwrapper')
                );
                $this->content->text .= html_writer::tag(
                    'div', 
                    $row['1'], 
                    array('class' => 'pguestbook_autor')
                );
                $this->content->text .= 
                    '<textarea name="text" class="pguestbook_text" disabled>';
                $this->content->text .= $row['3'];
                $this->content->text .= '</textarea>';
                $this->content->text .= html_writer::tag(
                    'div', 
                    $row['2'], 
                    array('class' => 'pguestbook_time')
                );
                $this->content->text .= 
                    '<input type="hidden" class="pguestbook_id" value="'.$row['0'].'" />';
                $this->content->text .= html_writer::tag(
                    'div', 
                    '', 
                    array('style' => 'clear: both;')
                );
                if ($USER->id == $row['8'] || $isadmin) {
                    $this->content->text .= html_writer::tag(
                        'span', 
                        get_string('delete', 'block_pguestbook'), 
                        array('class' => 'pguestbook_delete')
                    );
                    $this->content->text .= html_writer::tag(
                        'span', 
                        get_string('change', 'block_pguestbook'), 
                        array('class' => 'pguestbook_change')
                    );
                }
                $this->content->text .= html_writer::tag(
                    'span', 
                    get_string('add_comment', 'block_pguestbook'), 
                    array('class' => 'pguestbook_addc')
                );
                $this->content->text .= html_writer::end_tag('div');
                $this->content->text .= html_writer::tag(
                    'div', 
                    '', 
                    array('style' => 'clear: both;')
                );
            }
            // Комментарий
            if ($row['4'] != '') 
            {
                $this->content->text .= html_writer::start_tag(
                    'div', 
                    array('class' => 'pguestbook_commentwrapper')
                );
                $this->content->text .= html_writer::tag(
                    'div',
                    $row['5'], 
                    array('class' => 'pguestbook_autor')
                );
                $this->content->text .= 
                    '<textarea name="text" class="pguestbook_text" disabled>';
                $this->content->text .= $row['7'];
                $this->content->text .= '</textarea>';
                $this->content->text .= html_writer::tag(
                    'div', 
                    $row['6'], 
                    array('class' => 'pguestbook_time')
                );
                $this->content->text .= 
                    '<input type="hidden" class="pguestbook_id" value="'.$row['4'].'" />';
                $this->content->text .= html_writer::tag(
                    'div', 
                    '', 
                    array('style' => 'clear: both;')
                );
                if($USER->id == $row['9'] || $isadmin) {
                    $this->content->text .= html_writer::tag(
                        'span', 
                        get_string('delete', 'block_pguestbook'), 
                        array('class' => 'pguestbook_delete')
                    );
                    $this->content->text .= html_writer::tag(
                        'span', 
                        get_string('change', 'block_pguestbook'), 
                        array('class' => 'pguestbook_change')
                    );
                }
                $this->content->text .= html_writer::end_tag('div');
                $this->content->text .= html_writer::tag(
                    'div', 
                    '', 
                    array('style' => 'clear: both;')
                );
            } 
        }
        
        $this->content->text .= html_writer::end_tag('div');
        
            return $this->content;
        }
    }